- name: kubernetes.controller
  rules:
  - record: kube_controller_pod
    expr: |-
      max by(job, node, namespace, controller, controller_name, controller_type, pod)
      (
        label_replace (
        # lowercase ReplicaSet
          label_replace (
          # lowercase Deployment
            label_replace (
            # rename owner_kind to controller_type
              label_replace (
              # rename owner_name to controller_name
                label_replace (
                # rename owner_kind_name to controller
                  max by(job, namespace, replicaset, owner_kind_name, owner_kind, owner_name) (
                    label_replace (
                    # rename temp label to owner_kind_name
                      label_join (
                      # generate owner_kind_name
                        label_join (
                        # generate temp label for  owner_kind_name
                          label_replace(
                          # rename ReplicaSet
                            kube_replicaset_owner{owner_kind=~"Deployment|<none>"},
                            "replica", "ReplicaSet", "owner_kind", "<none>"),
                            "temp", "/", "replica", "replicaset"),
                            "owner_kind_name", "/", "owner_kind", "owner_name"),
                            "owner_kind_name", "$1", "temp", "(.+/.*)")
                    )
                   * on(job, namespace, replicaset) group_right(owner_kind, owner_name, owner_kind_name)
                   label_replace(
                   # add label replicaset for multiplication
                    max by(job, namespace, owner_name, pod) (
                      kube_pod_owner{owner_kind="ReplicaSet"})
                      * on (namespace, pod) group_left(node) sum by (node, namespace, pod) (kube_pod_info),
                   "replicaset", "$1", "owner_name", "(.*)"),
                 "controller", "$1", "owner_kind_name", "(.*)"),
               "controller_name", "$1", "owner_name", "(.*)"),
             "controller_type", "$1", "owner_kind", "(.*)"),
           "controller", "deploy/$1", "controller", "Deployment/(.*)"),
         "controller", "rs/$1", "controller", "ReplicaSet/(.*)")
      )
  - record: kube_controller_pod
    expr: |-
      sum by(job, node, namespace, controller, controller_type, controller_name, pod)
      (
        # certificate controller
        label_replace (
          # job controller
          label_replace (
            # sts controller
            label_replace (
              # ds controller
              label_replace (
                # none controller
                label_replace (
                  # controller_type for static pods
                  label_replace (
                    # controller
                    label_join (
                      # controller_name
                      label_replace (
                        # controller_type
                        label_replace (
                          kube_pod_owner{owner_kind!="ReplicaSet"}
                          * on (namespace, pod) group_left(node) sum by (node, namespace, pod) (kube_pod_info),
                        "controller_type", "$1", "owner_kind", "(.*)"),
                      "controller_name", "$1", "owner_name", "(.*)"),
                    "controller", "/", "controller_type", "controller_name"),
                  "controller_type", "No controller", "controller_type", "<none>"),
                "controller", "No Controller", "controller", "<none>/<none>"),
              "controller", "ds/$1", "controller", "DaemonSet/(.*)"),
            "controller", "sts/$1", "controller", "StatefulSet/(.*)"),
          "controller", "job/$1", "controller", "Job/(.*)"),
        "controller", "certificate/$1", "controller", "Certificate/(.*)")
      )
  - record: kube_controller_replicas
    expr: |-
     # get daemonset replicas
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_daemonset_status_desired_number_scheduled,
         "controller_type", "DaemonSet", "", ""),
       "controller_name", "$1", "daemonset", "(.*)")
     )
     or (
     # get deployment available replicas
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_deployment_spec_replicas,
         "controller_type", "Deployment", "", ""),
       "controller_name", "$1", "deployment", "(.*)"))
     )
     # get statefulset ready replicas
     or (
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_statefulset_replicas,
         "controller_type", "StatefulSet", "", ""),
       "controller_name", "$1", "statefulset", "(.*)"))
     )
     # get replicaset ready replicas
     or (
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_replicaset_spec_replicas,
         "controller_type", "ReplicaSet", "", ""),
       "controller_name", "$1", "replicaset", "(.*)"))
     )
  - record: kube_controller_replicas_ready
    expr: |-
     # get daemonset ready replicas
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_daemonset_status_number_ready,
         "controller_type", "DaemonSet", "", ""),
       "controller_name", "$1", "daemonset", "(.*)")
     )
     or (
     # get deployment available replicas
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_deployment_status_replicas_available,
         "controller_type", "Deployment", "", ""),
       "controller_name", "$1", "deployment", "(.*)"))
     )
     # get statefulset ready replicas
     or (
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_statefulset_status_replicas_ready,
         "controller_type", "StatefulSet", "", ""),
       "controller_name", "$1", "statefulset", "(.*)"))
     )
     # get replicaset ready replicas
     or (
     max by(job, namespace, controller_type, controller_name)
     (
       label_replace (
       # set controller_name
         label_replace (
         # set controller_type
           kube_replicaset_status_ready_replicas,
         "controller_type", "ReplicaSet", "", ""),
       "controller_name", "$1", "replicaset", "(.*)"))
     )
